package carltechnology.com.mahjonggame.presenter.impl;

import android.os.Message;

import carltechnology.com.mahjonggame.base.MyApplication;
import carltechnology.com.mahjonggame.mode.AbstractData;
import carltechnology.com.mahjonggame.mode.IData;
import carltechnology.com.mahjonggame.mode.event.FragmentViewEvent;
import carltechnology.com.mahjonggame.mode.impl.MahjongGameMode;
import carltechnology.com.mahjonggame.mode.impl.MahjongGameOutMode;
import carltechnology.com.mahjonggame.presenter.IMajhongGameInputPresenter;
import carltechnology.com.mahjonggame.utils.Utils;
import carltechnology.com.mahjonggame.utils.Utils.CONSTANT;
import carltechnology.com.mahjonggame.utils.dao.impl.MahjongDatabaseManager;
import carltechnology.com.mahjonggame.views.IMahjongGameInputView;
import carltechnology.com.mahjonggame.views.impl.MahjonggameInputFragment;
import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by WZTCM on 2016/6/7.
 */
public class MahjongGameInputPresenter implements IMajhongGameInputPresenter {
    private IMahjongGameInputView iMahjongGameInputView;
    private Subscriber<? super IData> subs;
    private Subscription subscription;
    private IData mode;

    private MahjongGameInputPresenter(final IMahjongGameInputView iMahjongGameInputView) {
        this.iMahjongGameInputView = iMahjongGameInputView;
        subscription = Observable.create(new Observable.OnSubscribe<IData>() {
            @Override
            public void call(Subscriber<? super IData> subscriber) {
                subs = subscriber;
            }
        })
                .onBackpressureBuffer(CONSTANT.ONBACKBUFFER_SMALL_SIZE.getConstant())
                .filter(new Func1<IData, Boolean>() {
                    @Override
                    public Boolean call(IData data) {
                        String[] names = data.getName();
                        MahjonggameInputFragment.MSG_TYPE resObj = null;
                        if (names[0] == null || names[0].isEmpty()) {
                            resObj = MahjonggameInputFragment.MSG_TYPE.PLAYER1_UNCORRECT;
                        } else if (names[1] == null || names[1].isEmpty()) {
                            resObj = MahjonggameInputFragment.MSG_TYPE.PLAYER2_UNCORRECT;
                        } else if (names[2] == null || names[2].isEmpty()) {
                            resObj = MahjonggameInputFragment.MSG_TYPE.PLAYER3_UNCORRECT;
                        } else if (names[3] == null || names[3].isEmpty()) {
                            resObj = MahjonggameInputFragment.MSG_TYPE.PLAYER4_UNCORRECT;
                        } else if (names[3] == null || names[3].isEmpty()) {
                            resObj = MahjonggameInputFragment.MSG_TYPE.PERUNIT_UNCORRECT;
                        } else if (data.getUnitPrice() == AbstractData.DATA_RES.NONE_UNIT_PRICE.getCode()) {
                            resObj = MahjonggameInputFragment.MSG_TYPE.PERUNIT_UNCORRECT;
                        }
                        if (resObj != null) {
                            Message message = new Message();
                            message.obj = resObj;
                            iMahjongGameInputView.getHandler().sendMessage(message);
                            return false;
                        }
                        return true;
                    }
                })
                .subscribeOn(Schedulers.computation())
                .subscribe(new Action1<IData>() {
                    @Override
                    public void call(IData data) {
                        storeMahjongGameInfo(data);
                        MahjongGameMode mode = MahjongGameMode._of(data.getId(),data.getSimpleName(),data.getDate());
                        MyApplication.getDataPool().setMahjongGameMode(mode);
                        Utils.getEventBus().post(new FragmentViewEvent(FragmentViewEvent.EVENT.EVENT_MAHJONG_DETAIL_FRAGMENT));
                        iMahjongGameInputView.getHandler().post(new Runnable() {
                            @Override
                            public void run() {
                                Utils.getEventBus().post(new FragmentViewEvent(FragmentViewEvent.EVENT.EVENT_MAHJONG_DETAIL_FRAGMENT));
                            }
                        });
                    }
                });
        MahjongDatabaseManager.getInstance().beginStoreMahjongGameInfo();

    }

    @Override
    public MahjongGameOutMode getMahjongGamOutMode() {
        String[] names = new String[4];
        names[0] = iMahjongGameInputView.getEditPlayer1();
        names[1] = iMahjongGameInputView.getEditPlayer2();
        names[2] = iMahjongGameInputView.getEditPlayer3();
        names[3] = iMahjongGameInputView.getEditPlayer4();
        String date = Utils.getTodayDate();
        String tmp = iMahjongGameInputView.getEditPerUnit();
        // unitPrice uninput
        int unitPrice = AbstractData.DATA_RES.NONE_UNIT_PRICE.getCode();
        if (tmp != null && !tmp.isEmpty()) {
            unitPrice = Integer.parseInt(tmp);
        }
        // System.out.println(unitPrice);
        MahjongGameOutMode mode = MahjongGameOutMode._of(names, date, unitPrice);
        return mode;
    }

    @Override
    public int getPerUnit() {
        return 0;
    }

    @Override
    public void examineAndStoreMahjongGameInfo() {
        IData data = getMahjongGamOutMode();
        subs.onNext(data);
    }

    @Override
    public void endFragment() {
        MahjongDatabaseManager.getInstance().endStoreMahjongGameInfo();
        subscription.unsubscribe();
    }
    private void storeMahjongGameInfo(IData data) {
        MahjongDatabaseManager.getInstance().storeMahjongGameInfo(data);
    }

    public static IMajhongGameInputPresenter newInstance(IMahjongGameInputView iMahjongGameInputView) {
        return new MahjongGameInputPresenter(iMahjongGameInputView);
    }


}
